<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;

class Official extends Controller
{
    public function index()
    {
        $adminId = session('uid','','admin');
        if(!$adminId)
        {
            return;
        }
        else
        {
            $type = input('type')?:203;
            //$list = Db::connect("live_db_config")->table('ims_account_wechats');//->where('type',$type);
            $list = Db::connect("live_db_config")
                        ->table('ims_account_wechats')
                        ->alias('w')
                        ->join('ims_account a','a.uniacid = w.uniacid')
                        ->field('w.*')
                        ->where('a.isdeleted',0)
                        ->where('w.type',$type);
            if(input('keyword'))
            {
                $list = $list->where('w.name','like','%'.input('keyword').'%');
            }
            
            if(input('status') != '')
            {
                if(input('status') != 2)
                    $list = $list->where('w.status',input('status'));
                else
                    $list = $list->where('red',1);
            }
            $list = $list->order('w.uniacid DESC')->select();
            $time = time();
            $voteCount = 0;
            foreach ($list as $k=>$v) {
                $voteCount += Db::connect("live_db_config")
                        ->table('ims_wzbagent_lives')
                        ->alias('l')
                        ->join('ims_wzbagent_vote_setting v','l.id = v.live_id')
                        ->where('l.uniacid',$v['uniacid'])
                        ->where('v.start_at','<',$time)
                        ->where('v.end_at','>',$time)
                        ->count(); 
            }
            $this->assign([
                'list'=>$list,
                'voteCount'=>$voteCount
            ]);
            return view();

            //query("SELECT * FROM ims_wzbagent_lives where id IN(4542,4545,4546,4547,4599);");
        }
    }
    
    public function edit()
    {
        if(request()->isAjax())
        {
            $data = input('post.')['data'];
            if(input('uniacid'))
            {
                $data['last_time'] = strtotime($data['last_time']);
                $sqlStatus = Db::connect("live_db_config")->table('ims_account_wechats')->where(['uniacid'=>input('uniacid')])->update($data);
            }
            else
            {
                //$sqlStatus = Db::connect("live_db_config")->table('ims_account_wechats')->insert($data);
            }
            if($sqlStatus  )
            {
                return ['msg'=>'操作成功','icon'=>1];
            }
            else
            {
                return ['msg'=>'操作失败','icon'=>2];
            }
        }
        else
        {
            if(input('uniacid'))
            {
                $data = Db::connect("live_db_config")->table('ims_account_wechats')->where(['uniacid'=>input('uniacid')])->find();
            }
            $staffList = db('admin')->where('group_id','IN','2,5')->where('status',1)->select();
            //if(!$wxappInfo['admin_name'])
            //    $wxappInfo['admin_name'] = db('admin')->where('id',$wxappInfo['admin_id'])->value('name');
            if($data['last_time'])
                $data['last_time'] = date('Y-m-d H:i:s' , $data['last_time']);
            $this->assign([
                'staffList'=>$staffList,
                'data'=>$data,
            ]);
            return view();
        }
    }
    
    public function del()
    {
        $sqlStatus = Db::connect("live_db_config")->table('ims_account')->where(['uniacid'=>input('uniacid')])->update(['isdeleted'=>1]);
        header('Location:'.$_SERVER['HTTP_REFERER']);
    }
    
    public function liveList()
    {
        if(input('uniacid'))
        {
            $uniacid = input('uniacid');
            $specialList = Db::connect("live_db_config")->table('ims_special')->where('uniacid',$uniacid)->order('special_id DESC')->select();
            $liveList =  Db::connect("live_db_config")
                        ->table('ims_wzbagent_lives')
                        ->alias('l')
                        ->join('ims_wzbagent_managers m','l.admin_id = m.id')
                        ->field('l.id,l.title,m.name')
                        ->where('l.uniacid',$uniacid)
                        ->order('l.id DESC')
                        ->select();
            foreach ($liveList as $k=>$v)
            {
                $voteSetting = Db::connect("live_db_config")->table('ims_wzbagent_vote_setting')->where('live_id',$v['id'])->find();
                $liveList[$k]['end_at'] = $voteSetting['end_at'];
                $liveList[$k]['start_at'] = $voteSetting['start_at'];
                if($liveList[$k]['end_at'] && $liveList[$k]['end_at']<time())
                {
                    $liveList[$k]['end_at'] = $liveList[$k]['start_at'] = '';
                    $liveList[$k]['end_text'] = '已结束';
                }
                    
            }
            array_multisort(array_column($liveList,'end_at'), SORT_DESC,$liveList);
            $this->assign([
                'specialList'=>$specialList,
                'liveList'=>$liveList,
            ]);
            return view();
        }
    }
    
    
    public function changeStatus()
    {
        if(request()->isAjax()){
            $uniacid=input('uniacid');
            $status = input('status');
            $field = input('field');
            if($status == 1){
                Db::connect("live_db_config")->table('ims_account_wechats')->where(['uniacid'=>$uniacid])->update([$field=>0]);
                return ['msg'=>'已禁用','icon'=>2];
            }else{
                Db::connect("live_db_config")->table('ims_account_wechats')->where(['uniacid'=>$uniacid])->update([$field=>1]);
                return ['msg'=>'已启用','icon'=>1];
            }
        }else{
            $this->error("非法操作！");
        }
    }
    

    
}